# -*- coding: UTF-8 -*-

# Copyright (C) 2009 Daniele Favara.
# Written by Daniele Favara <daniele@zeroisp.com>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


import os
import transaction
from tg import config, session, request
from tgext.zeroisp import model

from tgext.zeroisp.lib.picasa_google_com import PicasaGoogleCom

try:
    PATH_UPLOAD = os.path.join(config['pylons.paths']['static_files'],
                           'uploads')
except:
    print 'ERROR in PATH_UPLOAD'

SESSION_KEYS = ['address', 'post','post_meta', 'photos_todo', 'agency','posttype']

from pylons import session
from pylons.i18n.translation import set_lang, get_lang

def get_picasa():
    return PicasaGoogleCom(email=config.get('google.picasa.email'),
                         password=config.get('google.picasa.password'),
                         account_type=config.get('google.picasa.account_type'),
                         user=config.get('google.picasa.user'),
                         thumbsizes=[config.get('google.picasa.thumbsizes')],
                         imgmax = config.get('google.picasa.imgmax')
                               )

# -*- coding: utf-8 -*-
from httplib import HTTPConnection
from re import findall, DOTALL
def wikipedia(slug):
    #connect to a wikipedia (mediawiki) site (Polish)
    conn = HTTPConnection("it.wikipedia.org")
    #get page by slug
    conn.request('GET', '/wiki/'+slug)
    r = conn.getresponse()
    if str(r.status) == '200':
        #extract content
        tags = findall( r'<!-- start content -->(.*?)<!-- end content -->', r.read().decode('utf-8'), DOTALL)
        tags = tags[0].replace('href="/wiki', 'href="http://pl.wikipedia.org/wiki').replace('href="/w/', 'href="http://pl.wikipedia.org/w/')
    conn.close()
    return tags

def get_locale():
    langs = get_lang()
    locale = 'en'
    if langs is not None:
        locale = langs[0]
        if len(locale) > 2:
            locale = locale[:2]
    return locale


def set_lang_cookie(lang):
    session['lang'] = lang
    session.save()
